package com.xiaotu.code.system;

import java.io.Serializable;

import javax.jms.Message;
import javax.jms.MessageListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.jms.support.converter.MessageConverter;

/**
 * 队列消费者基类.
 * @author chenxiangrong
 *
 */
public abstract class AbstractQueneMessageConsumer implements MessageListener, QueneMessageConsumer, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7817984323894526668L;

	protected Logger logger = LoggerFactory.getLogger(getClass());

	private String destinationName;

	@Autowired
	private MessageConverter messageConverter;

	@Override
	public void onMessage(Message msg) {
		try {
			BaseQueneMessage message = (BaseQueneMessage) messageConverter.fromMessage(msg);
			this.receive(message);
		} catch (MessageConversionException e) {
			logger.error("消息转换错误，要从QueneBaseMessage继承下来才行", e);

		} catch (Exception e) {
			logger.error("不可意料的错误", e);
		}
	}

	@Override
	public String getDestinationName() {
		return this.destinationName;
	}

	public void setDestinationName(String name) {
		this.destinationName = name;
	}

}
